package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.user.IRegisterService;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Component
@Service
@Slf4j
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    IVerifyImpl iVerify;

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public UserRegisterResponse register(UserRegisterRequest request) {
        UserRegisterResponse response = new UserRegisterResponse();
        //判空验证
        request.requestCheck();

        //判断用户名是否重复
        volidUserNameRepeat(request);

        //向用户表中插入一条记录
        Member member = new Member();
        member.setUsername(request.getUserName());
        member.setEmail(request.getEmail());
        //密码加密
        String password = DigestUtils.md5DigestAsHex(request.getUserPwd().getBytes());
        member.setPassword(password);
        member.setCreated(new Date());
        member.setUpdated(new Date());
        member.setIsVerified("N");
        member.setState(1);
        int insert = memberMapper.insert(member);
        if (insert !=1){
            response.setCode(SysRetCodeConstants.USER_REGISTER_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.USER_REGISTER_FAILED.getMessage());
            return response;
        }

        //向用户验证表中插入一条记录
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(request.getUserName());
        userVerify.setRegisterDate(new Date());
        userVerify.setIsExpire("N");
        userVerify.setIsVerify("N");
        String s = request.getUserName() + UUID.randomUUID();
        String uuid = DigestUtils.md5DigestAsHex(s.getBytes());
        userVerify.setUuid(uuid);
        int insert1 = userVerifyMapper.insert(userVerify);
        if (insert1 !=1){
            response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getMessage());
            return response;
        }

        // TODO 发送用户激活邮件 消息队列MQ
        iVerify.sentEmail(request.getUserName(),uuid,member.getEmail());


        //注册成功
        log.info("注册参数：{}", JSON.toJSONString(request));
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;
    }

    private void volidUserNameRepeat(UserRegisterRequest request) {
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",request.getUserName());
        List<Member> members = memberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(members)){
            throw new ValidateException(SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getCode(),SysRetCodeConstants.USERNAME_ALREADY_EXISTS.getMessage());
        }
    }
}
